/**
 *  The Yz.Session prototype
 */
(function() {
    var Yz = window.Yz;
    if (! (Yz instanceof Function)) {
        throw "Failed to identify the root Yz object";
    }

    Yz.Session = function()
    {
        //return this.init();
    };

    // notify the root Yz object that we have loaded
    Yz.loadedClasses["Yz.Session"] = Yz.Session;


    /****************************
     *      Implementation      *
     ****************************/

    Yz.Session.expDate = 0;
    Yz.Session.timeout = 0;

    Yz.Session.startTimer = function(startSecs, secsLeft)
    {
        console.log("start timer");
        var self = Yz.Session;
        self.outputTimer(new Date(startSecs*1000), secsLeft);
        self.expDate = new Date( (startSecs+secsLeft)*1000 );
        self.timeout = window.setInterval(
            function() {
                var d = new Date();
                var delta = Math.floor( (self.expDate - d.getTime())/1000 );
                if (delta >= 0) {
                    self.outputTimer(d, delta);
                } else {
                    window.clearInterval(self.timeout);
                }
            },
            1000
        );
    }

    Yz.Session.outputTimer = function(date, delta)
    {
        $('#yz-admin-time').text(date.toLocaleString());
        var hr = Math.floor(delta/3600);
        var min = Math.floor( (delta%3600) / 60 );
        var sec = delta%60;
        $('#yz-admin-session-expires').text(
            hr+":"+
            (min<10 ? "0"+min : min)+":"+
            (sec<10 ? "0"+sec : sec)
        );
    }

})();
